package com.dk.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dk.mapper.WarehouseMapper;
import com.dk.pojo.User;
import com.dk.pojo.Warehouse;
import com.dk.pojo.WarehouseExample;
import com.dk.result.PageResult;
import com.dk.result.Result;
import com.dk.result.ResultConstant;
import com.dk.service.WarehouseService;
import com.dk.utils.JwtUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class WarehouseServiceImpl implements WarehouseService {

    @Autowired
    private WarehouseMapper warehouseMapper;
    @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    public PageResult<Warehouse> getWarehouseInfo(Integer pageNum, Integer pageSize, String code, String name) {
        //redis里没有
        if(!redisTemplate.hasKey("mesWarehouse"+code+name)){
            WarehouseExample example = new WarehouseExample();
            if(code != null && code.length() > 0){
                example.createCriteria().andCodeEqualTo(code);
            }
            if( name != null && name.length() > 0){
                example.createCriteria().andNameLike("%"+name+"%");
            }
            List<Warehouse> warehouseList = warehouseMapper.selectByExample(example);
            redisTemplate.boundValueOps("mesWarehouse"+code+name).set(JSONObject.toJSONString(warehouseList));
            return getWarehousePageResult(pageNum, pageSize, warehouseList);
        }else {
            String o = (String) redisTemplate.boundValueOps("mesWarehouse" + code + name).get();
            List<Warehouse> warehouses = JSONObject.parseArray(o, Warehouse.class);
            return getWarehousePageResult(pageNum, pageSize,warehouses);

        }

    }

    //抽取的分页方法
    private PageResult<Warehouse> getWarehousePageResult(Integer pageNum, Integer pageSize, List<Warehouse> warehouseList) {
        PageHelper.startPage(pageNum, pageSize);
        PageInfo<Warehouse> pb = new PageInfo<>(warehouseList);
        PageResult<Warehouse> result = new PageResult<>(pb.getPageNum(), pb.getPageSize(), pb.getTotal(),pb.getList());
        return result;
    }

    @Override
    public Result addWarehouse(Warehouse warehouse,String token) {
        try {
            Claims claims = jwtUtil.parseJwt(token);
            String subject = claims.getSubject();
            User user = JSONObject.parseObject(subject, User.class);
            warehouse.setAddPerson(user.getUsername());
            warehouse.setAddDate(new Date());
            warehouseMapper.insertSelective(warehouse);
            return  new Result(ResultConstant.CODE,ResultConstant.MESSAGE);
        } catch (Exception e) {
            return new Result(ResultConstant.TOKEN_CODE, ResultConstant.TOKEN_MESSAGE);
        }
    }

    @Override
    public Result updateWarehouse(Warehouse warehouse, String token) {
        try {
            //通过jwtUtil解析token字符串
            Claims claims = jwtUtil.parseJwt(token);
            String subject = claims.getSubject();
            //转成user对象
            User user = JSONObject.parseObject(subject, User.class);
            warehouse.setUpdatePerson(user.getUsername());
            warehouse.setUpdateDate(new Date());
            warehouseMapper.updateByPrimaryKeySelective(warehouse);
            return  new Result(ResultConstant.CODE,ResultConstant.MESSAGE);
        } catch (Exception e) {
            //失败返回错误信息
            return new Result(ResultConstant.TOKEN_CODE, ResultConstant.TOKEN_MESSAGE);
        }
    }

    @Override
    public void deleteWarehouse(Integer wid) {
        warehouseMapper.deleteByPrimaryKey(wid);
    }

    @Override
    public Warehouse selectByWarehouseName(String warehouseName) {
        WarehouseExample example = new WarehouseExample();
        example.createCriteria().andNameEqualTo(warehouseName);
        List<Warehouse> warehouses = warehouseMapper.selectByExample(example);
        return warehouses.size() > 0 ? warehouses.get(0): null;
    }
}